let token = sessionStorage.getItem("token");

    //初始化总页数
    var pages =1;
    //获取列表
    function loadData(pageNum){
        $.ajax({
            url:testurl + "duty/collection",
            type:"post",
            headers: { "Authorization": "Bearer "+token},
            contentType: "application/json",
            data:JSON.stringify({
                "pageNum": pageNum
            }),
            success: function (data) {
                console.log(data)
                pages = Math.ceil(data.pageTotal/data.pageSize);
                var record = data.obj.slice((pageNum-1)*15,pageNum*15);
                 str = "";
                $.each(record ,function (index,values) {
                    console.log(str);
                    str += " <tr class=\"gradeX\" data-type=\""+values.type+"\" onclick=\"check("+values.id+")\" data-am-modal=\"{target: '#doc-modal-train', closeViaDimmer: 1, width: 1000, height: 1150}\">\n" +
                        "<td class=\"title\" data-type=\""+values.type+"\">"+values.title+"</td>\n" +
                        "<td class=\"date\">"+values.create_times+"</td>\n" +                       
                        "</tr>"
                });
                $("#show").html(str);
                $("#show tr td[data-type='1']").append("<i class=\"fa fa-youtube-play\" aria-hidden=\"true\"></i>");
                $("#show tr[data-type='1']").attr('data-am-modal','{target: \'#doc-modal-video\', closeViaDimmer: 1, width: 1350, height: 800}');
                $(".pageNum").html(pageNum);
            }
        });
    }

    $(function () {
        loadData(1);
        //分页
        var pageNum = 1;
        $(".first").click(function(){
         pageNum =1;
            loadData(pageNum);
            $(this).parents("li").addClass("am-active");
            $(this).parents("li").siblings().removeClass("am-active");
        });
        $(".last").click(function(){
            pageNum = pages;
            loadData(pageNum);
            $(this).parents("li").addClass("am-active");
            $(this).parents("li").siblings().removeClass("am-active");
        });
        $(".up").click(function(){
            if(pageNum === 1){
                return false;
            } else{
                pageNum--;
                loadData(pageNum);
            }
            $(this).parents("li").addClass("am-active");
            $(this).parents("li").siblings().removeClass("am-active");
        });
        $(".down").click(function(){
            if(pageNum === pages){
                return false;
            } else{
                pageNum++;
                loadData(pageNum);
            }
            $(this).parents("li").addClass("am-active");
            $(this).parents("li").siblings().removeClass("am-active");
        });
    })

    //获取被查看记录的数据并获取评论列表
    var is_collect = 2;
    function check(id){
        var type;        
        $.ajax({
            url: testurl + "duty/detail",
            type: "POST",
            async: false,
            headers: { "Authorization": "Bearer "+token},
            contentType: "application/json",
            data: JSON.stringify({
                "id": id
            }),
            success: function (data) {
                type =data.obj.type;
                is_collect = data.obj.is_collect;
                if(is_collect==1){
                    $(".collect i").removeClass("fa-star-o").addClass("fa-star");
                    $(".collect span").html("已收藏");
                }else{
                    $(".collect i").removeClass("fa-star").addClass("fa-star-o");
                    $(".collect span").html("未收藏");
                }
                if(data.obj.type==2){
                    $("#doc-modal-train #id").html(data.obj.id);
                    $("#doc-modal-train #title").html(data.obj.title);
                    $('#doc-modal-train #content').html(data.obj.content); 
                }else{
                    var url = testurl + "";
                    $("#doc-modal-video #id").html(data.obj.id);
                    $("#doc-modal-video #title").html(data.obj.title);
                    $('#doc-modal-video #video').attr('src', ""+url+""+data.obj.file_list[0].path+"");
                }
            }
        });    
        //获取评论列表   
        commentList(id,type);
    };
   
    //收藏
    function collect(modalId){
        var collectData = {};
        collectData.duty_id = $(""+modalId+" #id").html();
        if(is_collect==1){
            is_collect = 2;
            collectData.flag = "2"; 
            $(""+modalId+" .collect i").removeClass("fa-star").addClass("fa-star-o");
            $(""+modalId+" .collect span").html("未收藏");
        }else{
            is_collect = 1;
            collectData.flag = "1";
            $(""+modalId+" .collect i").removeClass("fa-star-o").addClass("fa-star");
            $(""+modalId+" .collect span").html("已收藏");
        }     
        //console.log(collectData);
        $.ajax({
            url:testurl + "duty/collect",
            type:"post",
            headers: { "Authorization": "Bearer "+token},
            contentType: "application/json",
            data:JSON.stringify(
                collectData
            ),
            success: function (data) {
                
                console.log(data.message);
                
            }
        });
    }

    //获取评论列表函数
    function commentList(id,type){     
        $.ajax({
            url:testurl + "duty/comment/list",
            type:"post",
            async: false,
            headers: { "Authorization": "Bearer "+token},
            contentType: "application/json",
            data:JSON.stringify({
                "duty_id":id,"pageNum": 1
            }),
            success: function (data) {
                //console.log(type);
                var record;
                if(type==2){
                    record = data.obj.slice(0,2);
                }else{
                    record = data.obj.slice(0,4);
                }
                var str = "";
                var url = testurl + ""
                $.each(record ,function (index,values) {
                    str += "<div class=\"list-item\" ><div class=\"head\"><i class=\"fa fa-user-circle-o\" aria-hidden=\"true\"></i><span class=\"title\">"+values.create_user_name+"</span><span class=\"date\">"+values.create_times+"</span></div><div class=\"content\">"+values.content+"</div></div>"
                });
                if(type==2){
                    $("#doc-modal-train #comment-list").html(str);
                }else{
                    $("#doc-modal-video #comment-list").html(str);
                }
            }
        });
    };

    //发表评论   
    function publish(modalId,type){
        var commentData = {};
        commentData.duty_id = $(""+modalId+" #id").html();
        commentData.content = $(""+modalId+" #comment-content").val();
        if(commentData.content){
            $.ajax({
                url:testurl + "duty/comment/",
                type:"post",
                async:false,
                headers: { "Authorization": "Bearer "+token},
                contentType:"application/json",
                data:JSON.stringify(
                    commentData
                ),
                success:function(data){
                    console.log(data.message);
                    $(""+modalId+" #comment-content").val("")
                    commentList(commentData.duty_id,type);
                },
                
            });
            return false;
        }else{
            alert("请输入您的评价内容");
            return false;
        }
    }